using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using System.Net;

namespace MediaPortal.GUI.iPlayer
{
    public class iplay_series : GUIWindow
    {
        #region SkinControlAttributes
        [SkinControlAttribute(10)] 
        protected GUIListControl listview = null;
        [SkinControlAttribute(3)]
        protected GUIImage image = null;
        Preferences prefs = new Preferences();
        #endregion

        #region Variables
        public static string Url = string.Empty;
        public TVProgramList myList = new TVProgramList();
        private common_classes commonStuff = new common_classes();   //some common classes
        
        private List<TVProgram> Episodes = new List<TVProgram>();
        #endregion

        public override int GetID
        {
            get
            {
                return 5679;
            }
            set
            {
                
            }
        }

        public override bool Init()
        {
            myList.LoadFromFile();
            return Load(GUIGraphicsContext.Skin + @"\iPlay_navigate_BBC.xml");

        }

        protected override void OnPageLoad()
        {

            GetInfo(Url);
            Showlist();
            base.OnPageLoad();  
        }

        protected override void OnPageDestroy(int new_windowId)
        {
            GUIPropertyManager.SetProperty("#title", " ");

            image.FileName = GUIGraphicsContext.Skin + @"\media\background.png";
            Episodes.Clear();
            base.OnPageDestroy(new_windowId);
        }

        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == listview)
            {

                TheWebThing.prog_nav = "http://www.bbc.co.uk/iPlayer/episode/" + Episodes[listview.SelectedListItemIndex].ProgramId;
                //TheWebThing.prog_nav = Episodes[listview.SelectedListItemIndex].ProgramLink;
                //TheWebThing.prog_nav = "about:config";

                
    //            GUIDialogWindow stuff = (GUIDialogWindow)GUIWindowManager.GetWindow(
    //(int)MediaPortal.GUI.iPlayer.TheWebThing.WINDOW_WEB_BROWSER);
                
      //          stuff.DoModal(GUIWindowManager.ActiveWindow);
                
                GUIWindowManager.ActivateWindow(5500);

               
                
            }
            base.OnClicked(controlId, control, actionType);
        }

        public override bool OnMessage(GUIMessage message)
        {
            if (message.Message == GUIMessage.MessageType.GUI_MSG_ITEM_FOCUS || message.Message == GUIMessage.MessageType.GUI_MSG_ITEM_FOCUS_CHANGED)
            {
                if (listview.SelectedListItemIndex > -1)
                {
                   // GUIPropertyManager.SetProperty("#rating", "" + Episodes[listview.SelectedListItemIndex].  .ProgramGenre);
                    GUIPropertyManager.SetProperty("#plotoutline", Episodes[listview.SelectedListItemIndex].ProgramSummary);
                }
            }
            return base.OnMessage(message);
        }

        //Summary get the list of programs based on URl from bbc_top.
        private void GetInfo(string url)
        {
            foreach (TVProgram ep in myList.thePrograms)
            {
                if (ep.ProgramName == url)
                {
                    Episodes.Add(ep);
                }
            }

            foreach (TVProgram ep in myList.thePrograms)
            {
                if (ep.ProgramName == url)
                {
                    GUIPropertyManager.SetProperty("#title", ep.ProgramName);
                    GUIPropertyManager.SetProperty("#genre", ep.ProgramGenre);
                    GUIPropertyManager.SetProperty("#credits", ep.ProgramProvider);
                    
                    //get image
                    image.FileName =  commonStuff.DownloadImage(ep.ProgramImage);

                    break;
                }
            }

            GUIPropertyManager.SetProperty("#plot", "");
        }


        private void Showlist()
        {
            listview.Clear();

          foreach (TVProgram ep in Episodes)
            {
                if (ep.ProgramName == Url)
                {
                    GUIListItem item = new GUIListItem();
                    item.IconImage = "defaultVideo.png";
                    item.Label = ep.ProgramName;
                    item.Label2 = "Episode: " +ep.ProgramEpisode;
                    listview.Add(item);
                }
            }
        }

        //public string DownloadImage(string url)
        //{
        //    // Log.Info("TVGemist: Trying to get following image: {0}", url.ToString());

        //    try
        //    {
        //        string thumbsDir = MediaPortal.Configuration.Config.GetFolder(MediaPortal.Configuration.Config.Dir.Thumbs);

        //        WebClient wc = new WebClient();
        //        string filename = url.Substring(url.LastIndexOf("/") + 1);

        //        wc.DownloadFile(url, thumbsDir + @"\MPTemp-" + filename);
        //        while (System.IO.File.Exists(thumbsDir + @"\MPTemp-" + filename) != true)
        //            GUIWindowManager.Process();
        //        return thumbsDir + @"\MPTemp-" + filename;
        //    }
        //    catch (Exception e)
        //    {
        //        Log.Info(e.Message);
        //        return string.Empty;
        //    }
        //}

    }
}
